#ifndef _SDIO_H
#define _SDIO_H

#include <linux/types.h>
#include <linux/init.h>
#include <linux/kernel.h>

/** Length of device name */
#define DEV_NAME_LEN                32

#define FIFO_ADDRESS                0x00000000
#define SDIO_TRANS_MAX_SZIE         (64*1024)

#define SDIO_MAILBOX_CHANNEL_COUNT  (2) //total 4, but 2 reserved for send

#define WORKAROUND_FOR_INTERRUPT_LOST_ISSUE

struct artosyn_rw_args {
    unsigned int addr;
    unsigned int length;
    unsigned char val;
};

struct artosyn_msg_args {
    unsigned char valid[SDIO_MAILBOX_CHANNEL_COUNT];
    unsigned char message[SDIO_MAILBOX_CHANNEL_COUNT];
};

struct artosyn_cmd_args {
    unsigned int opcode;
    unsigned int arg;
    unsigned int flags;
    unsigned int retries;
    unsigned int resp[4];
};

#define CMD             _IOWR('v', 0, struct artosyn_cmd_args)
#define READ_BYTE       _IOR('v', 1, struct artosyn_rw_args)
#define WRITE_BYTE      _IOW('v', 1, struct artosyn_rw_args)
#define READ_MESSAGE    _IOWR('v', 2, struct artosyn_msg_args)
#define WRITE_MESSAGE   _IOW('v', 2, struct artosyn_msg_args)
#define READ_F0_BYTE    _IOR('v', 3, struct artosyn_rw_args)
#define WRITE_F0_BYTE   _IOW('v', 3, struct artosyn_rw_args)

/* SDIO FUNCs*/
#define SDIO_FUNC_0        0
#define SDIO_FUNC_1        1
#define SDIO_FUNC_2        2
#define SDIO_FUNC_3        3
#define SDIO_FUNC_4        4
#define SDIO_FUNC_5        5
#define SDIO_FUNC_6        6
#define SDIO_FUNC_7        7
#define SDIO_FUNC_MAX      7
#define SDIO_FUNC_SIZE     8

#define SDIO_FUNC_ARTOSYN         SDIO_FUNC_1

/** Card Common Control Registers (CCCR) */
#define SDIO_CCCR_SDIO_VERSION    0x00
#define SDIO_CCCR_SD_VERSION      0x01
#define SDIO_CCCR_IO_ENABLE       0x02
#define SDIO_CCCR_IO_READY        0x03
#define SDIO_CCCR_INT_ENABLE      0x04
#define SDIO_CCCR_INT_PENDING     0x05
#define SDIO_CCCR_IO_ABORT        0x06
#define SDIO_CCCR_BUS_CONTROL     0x07
#define SDIO_CCCR_CARD_CAP        0x08
#define SDIO_CCCR_CIS_PTR         0x09
#define SDIO_CCCR_BUS_SUSPEND     0x0c
#define SDIO_CCCR_FUNC_SEL        0x0d
#define SDIO_CCCR_EXEC_FLAG       0x0e
#define SDIO_CCCR_READY_FLG       0x0f
#define SDIO_CCCR_BLK_SIZE        0x10
#define SDIO_CCCR_PWR_CONTROL     0x12
#define SDIO_CCCR_BUS_SPEED_SEL   0x13
#define SDIO_CCCR_UHSI_SUPPORT    0x14
#define SDIO_CCCR_DRV_STRENGTH    0x15
#define SDIO_CCCR_INT_EXTERN      0x16

/**sdio write/read action*/
#define SDIO_ACT_WRITE    1
#define SDIO_ACT_READ     0

#define SDIO_BLOCK_ALIGN_SIZE   0x1000

#define ARTOSDIO_MAGIC_HEADER_1  'S'
#define ARTOSDIO_MAGIC_HEADER_2  'D'

/* artosyn sdio protocol */
/* assume sdio block size 512 */
#define    SDIO_DEVICE_BLOCK_SIZE        (512)
#define    SDIO_TEST_SIZE                (1024*64)
#define    SDIO_BOOT_ONESHOT_MAX_SIZE    (1024*4)
// #define    SDIO_DEVICE_NAME              "/dev/artosyn_sdio"
#define    SDIO_LOG_LOOP_CNT             (1024)
#define    SDIO_BOOT_PKG_HEADER_SIZE     (12)

#define SDIO_MAILBOX_CHANNEL_COUNT  (2) //total 4, but 2 reserved for send

void artosyn_sdio_stats_str(char* buf, int buf_len);
int sdio_artosyn_init(void);
void sdio_artosyn_exit(void);

#endif